<!--
 * @Author: your name
 * @Date: 2021-12-25 11:32:33
 * @LastEditTime: 2021-12-25 12:20:53
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \shop\src\components\user\address\add.vue
-->
<template>
    <!-- 导航栏 -->
    <van-nav-bar title="新增收货地址" left-text="返回" left-arrow @click-left="back" />

    <!-- 新增地址 -->
    <van-address-edit
    :area-list="areaList"
    show-set-default
    :area-columns-placeholder="['请选择', '请选择', '请选择']"
    :address-info="AddressInfo"
    @save="onSave"
    />
</template>
<script>
    import { areaList } from '@vant/area-data'
    export default {
        name: 'AddressAdd',
        emits: ['toggle'],
        created() {
            //触发父组件的事件
            this.$emit('toggle', false)
        },
        data(){
            return {
                areaList,
                LoginUser: this.$cookies.get('LoginUser')
            }
        },
        computed:{
            AddressInfo(){
                var data = {
                    name: this.LoginUser.nickname
                }

                var area = this.LoginUser.region.split('-')

                //判断是否为数组
                if(!Array.isArray(area))
                {
                    //如果不是数组就默认给一个空数组
                    area = []
                }
                //判断数组里面是否有第一个元素
                if(area[0])
                {
                    data.province = area[0]
                    data.areaCode = this.LoginUser.province.toString()
                }

                if(area[1])
                {
                    data.city = area[1]
                    data.areaCode = this.LoginUser.city.toString()
                }

                if(area[2])
                {
                    data.county = area[2]
                    data.areaCode = this.LoginUser.district.toString()
                }

                return data
            }
        },
        methods: {
            back() {
                this.$router.go(-1);
            },
            //保存新增地址
            async onSave(content) {
                //组装数据
                var data = {
                    consignee: content.name,
                    mobile: content.tel,
                    code: content.areaCode,
                    address: content.addressDetail,
                    status: content.isDefault ? 1 : 0,
                    userid: this.LoginUser.id
                }

                var result = await this.$api.AddressAdd(data)

                if(result.code == 1){
                     //提醒信息 提醒完之后返回上一层
                    this.$notify({
                        type:'success',
                        message:result.msg,
                        duration:1000,
                        onClose: () => {
                        this.$router.go(-1)
                        }
                    })
                }else{
                    //添加失败
                    this.$notify({
                        message:result.msg,
                        duration:1000,
                    })
                }

            }
        }
    }
</script>